var Menu = function() {
	var menuContainerId = "menu";
	var $menuContainer = $("#" + menuContainerId);
	
	this.init = function() {
		$.ajax({
		    url: "league/getList",
			type: "POST",
			data: {},
			dataType: "json",
			async: true,
			success: [initSuccess],
			beforeSend: beforeInit
		});		
	};	
	
	var initSuccess = function(data, textStatus, jqXHR) {
		if (textStatus == "success") {
			displayLeagueMenu(data);
		}
	};
		
	var beforeInit = function(jqXHR, settings) {
			
	};
	
	var displayLeagueMenu = function(data) {
		for (var i = 0; i < data.length; i++) {
			$menuElement = createLeagueMenuElement(data[i]);
			$menuContainer.append($menuElement);
		}
	}
	
	var createLeagueMenuElement = function(menuData) {
		var $menuElement = $(document.createElement("div"));
		$menuElement.attr("league_id", menuData.league_id);
		$menuElement.html(menuData.name);
		$menuElement.addClass("menu_item");
		$menuElement.click(leagueMenuElementClick);
		
		return $menuElement;
	}
	
	var leagueMenuElementClick = function(event) {
		var $menuElement = $(event.target);
		new Match($menuElement.attr("league_id"));		
	}
}